<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeRoleDia">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <div style="text-align: right">
        <el-button type="primary" size="small" @click="assignRoles">确定</el-button>
        <el-button size="small" @click="closeRoleDia">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListAPI } from "@/api/setting"
import { getUserAvatarApi } from "@/api/user"
import { assignRolesAPI } from "@/api/employees";

export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    },
  },
  data() {
    return {
      list: [],
      roleIds: [],
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 分配员工角色
    async assignRoles() {
      // console.log('角色分配权限', this.roleIds)
      if (!this.roleIds.length) {
        return this.$message.warning('至少选则一个权限!')
      }
      await assignRolesAPI({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('修改权限成功')
      this.closeRoleDia()
    },
    // 获取当前权限角色
    async getUserAvatar(id) {
      const res = await getUserAvatarApi(id)
      console.log(res)
      this.roleIds = res.data.roleIds
    },
    // 获取角色列表
    async getRoleList() {
      const res = await getRoleListAPI(1, 100)
      this.list = res.data.rows
      // console.log('角色列表', res.data.rows)
    },
    // 关闭控制权限弹框
    closeRoleDia() {
      this.$emit('update:showRoleDialog', false)
    }
  },
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>